package com.feng.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.reggie.common.CustomException;
import com.feng.reggie.dto.DishDto;
import com.feng.reggie.entity.Dish;
import com.feng.reggie.entity.DishFlavor;
import com.feng.reggie.entity.Setmeal;
import com.feng.reggie.entity.SetmealDish;
import com.feng.reggie.mapper.DishMapper;
import com.feng.reggie.service.DishFlavorService;
import com.feng.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品Service实现
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应菜品口味数据
     *
     * @param dishDto
     */
    @Override
    //涉及到多张表的操作，所以要加上事务控制注解，保证数据的一致性,要么两张表都修改成功，要么同时失败，防止一张表成功另一个失败的情况出现
    // 要生效则需要在启动类上用@EnableTransactionManagement注解开启事务支持
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish中
        this.save(dishDto);

        //获得保存菜品后雪花算法生成的id
        Long dishId = dishDto.getId();

        //取出菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        //遍历flavors为每一个菜品口味flavor加工上dishId
        List<DishFlavor> newFlavors = flavors.stream().map((flavor) -> {
            flavor.setDishId(dishId);
            return flavor;
        }).collect(Collectors.toList());

        //保存菜品口味的信息到菜品口味表dish_flavor中
        dishFlavorService.saveBatch(newFlavors);
    }


    /**
     * 根据id查询菜品信息和对应的口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlovor(Long id) {

        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(DishFlavor::getDishId, id);

        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(flavors);

        return dishDto;
    }


    /**
     * 更新菜品信息同时更新口味信息
     * @param dishDto
     */
    @Override
    //涉及到多张表的操作，所以要加上事务控制注解，保证数据的一致性
    // 要生效则需要在启动类上用@EnableTransactionManagement注解开启事务支持
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品口味数据---dish_falvor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        //添加当前菜品口味数据---dish_falvor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        //遍历flavors为每一个菜品口味flavor加工上dishId
        List<DishFlavor> newFlavors = flavors.stream().map((flavor) -> {
            flavor.setDishId(dishDto.getId());
            return flavor;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(newFlavors);
    }

    @Override
    @Transactional
    public void deleteWithFlavor(List<Long> ids) {

        //查询菜品状态，确定是否可以删除（在售不可删除，停售可删除）
        LambdaQueryWrapper<Dish> setmealQueryWrapper1 = new LambdaQueryWrapper<>();
        setmealQueryWrapper1.in(Dish::getId,ids);
        setmealQueryWrapper1.eq(Dish::getStatus,1);

        //如果不能删除，抛出一个异常
        if(this.count(setmealQueryWrapper1)>0){
            throw new CustomException("菜品正在售卖中，不能删除，请先停售。");
        }

        //如果可以删除，先删除菜品对应的口味数据
        LambdaQueryWrapper<DishFlavor> dishFlavorQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(dishFlavorQueryWrapper);

        //再删除菜品表中的数据
        this.removeByIds(ids);

    }


}
